#ifndef __LANQIN_H__
#define __LANQIN_H__

#define true 1
#define flase 0
#define null 18
#define LED_SD P0 //LED数码管显示编码接口
sbit LED_SD_1 = P2^4; //LED数码管开关
sbit LED_SD_2 = P2^5; 
sbit LED_SD_3 = P2^6; 
sbit LED_SD_4 = P2^7; 
sbit LED1 = P1^0; //LED灯
sbit LED2 = P1^1; 
sbit LED3 = P1^2; 
sbit LED4 = P1^3; 
sbit LED5 = P1^4; 
sbit LED6 = P1^5; 
sbit LED7 = P1^6; 
sbit LED8 = P1^7; 
sbit buzzer = P2^3; //蜂鸣器
sbit K1 = P3^4; //独立按键
sbit K2 = P3^5;
sbit K3 = P3^6;
sbit K4 = P3^7;

//共阴极LED显示器字形码，0不带点，1带点,0-9 A-F 熄灭
unsigned char code number[2][17] = {
    {0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,0x77,0x7C,0x39,0x5E,0x79,0x71,0x00},
    {0xBF,0x86,0xDB,0xCF,0xE6,0xED,0xFD,0x87,0xFF,0xEF,0xF7,0xFC,0xB9,0xDE,0xF9,0xF1,0x80}
    };

//延时函数群，适用于不同的时间段
//兼容老的文件
void delay(unsigned int ms){
    unsigned int i,j;
    for(i = ms;i;i--){
        for(j = 112;j;j--) ;
    }
}
//毫秒延时
void delay_ms(unsigned int ms){
    unsigned int ms_i,ms_j;
    for(ms_i = ms;ms_i;ms_i--){
        for(ms_j = 112;ms_j;ms_j--) ;
    }
}
//秒延时
void delay_s(unsigned int s){
    unsigned int s_i;
    for(s_i = s;s_i;s_i--) delay_ms(1000);
}
//分钟延时
void delay_m(unsigned int m){
    unsigned int m_i;
    for(m_i = m;m;m--) delay_s(60);
}
//小时延时
void delay_h(unsigned int h){
    unsigned int h_i;
    for(h_i = h;h_i;h_i--) delay_m(60);
}

//LED数码管动态显示切换主显
void open_LED_SD_1(){
    LED_SD_2 = LED_SD_3 = LED_SD_4 = flase;
    LED_SD_1 = true;
}
void open_LED_SD_2(){
    LED_SD_1 = LED_SD_3 = LED_SD_4 = flase;
    LED_SD_2 = true;
}
void open_LED_SD_3(){
    LED_SD_1 = LED_SD_2 = LED_SD_4 = flase;
    LED_SD_3 = true;
}
void open_LED_SD_4(){
    LED_SD_1 = LED_SD_2 = LED_SD_3 = flase;
    LED_SD_4 = true;
}

//LED数码管显示功能封装
//首位为0，小数点灭 | 首位为1，小数点亮
void LED(int deta [5]){
    if(deta[0] == null) {
            delay(5);
            open_LED_SD_1();
            LED_SD = number[0][16];
    }
    if (deta[1] != null) {
            delay(5);
            open_LED_SD_1();
            LED_SD = number[deta[0]][deta[1]];
    }
    if (deta[2] != null) {
            delay(5);
            open_LED_SD_2();
            LED_SD = number[deta[0]][deta[2]];
    }
    if (deta[3] != null) {
            delay(5);
            open_LED_SD_3();
            LED_SD = number[deta[0]][deta[3]];
    }
    if (deta[4] != null) {
            delay(5);
            open_LED_SD_4();
            LED_SD = number[deta[0]][deta[4]];
    }
}
//LED数码管单个显示功能封装
//首位数为几，第几位带小数点显示
void LED_single(int deta [5]){
    int LED_Single_i = deta[0];

    open_LED_SD_1();
    if (LED_Single_i == 1) LED_SD = number[1][deta[1]];
    else LED_SD = number[0][deta[1]];
    
    delay_ms(1);
    open_LED_SD_2();
    if (LED_Single_i == 2) LED_SD = number[1][deta[2]];
    else LED_SD = number[0][deta[2]];

    delay_ms(1);
    open_LED_SD_3();
    if (LED_Single_i == 3) LED_SD = number[1][deta[3]];
    else LED_SD = number[0][deta[3]];

    delay_ms(1);
    open_LED_SD_4();
    if (LED_Single_i == 4) LED_SD = number[1][deta[4]];
    else LED_SD = number[0][deta[4]];
    delay_ms(1);
}

//数码管多组数据自动显示
//数据传输使用多维数组，其最后一个数组全为-1以示结尾
void Auto_LED(int deta[255][5]){
    int Auto_LED_i,Auto_LED_j;
    for(Auto_LED_i = 0;deta[Auto_LED_i][1] != -1;Auto_LED_i++)
        for(Auto_LED_j = 50;Auto_LED_j;Auto_LED_j--) LED(deta[Auto_LED_i]);
}

#endif